/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Form;

import BLL.BLLImport;
import BLL.BLLManu;
import BLL.BLLProduct;
import DTO.DTOImport;
import DTO.DTOProduct;
import java.awt.event.KeyEvent;
import java.util.Date;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Pham
 */
public class JPanelMangeImport extends javax.swing.JPanel {
    BLLManu bllManu;
    BLLProduct bllPro;
    BLLImport bllImp;
    DefaultTableModel modelImp;
    DefaultTableModel modelPro;
    /**
     * Creates new form NewJPanel1
     */
    public JPanelMangeImport() {
        bllManu = new BLLManu();
        bllPro = new BLLProduct();
        bllImp = new BLLImport();
        initComponents();
        
        loadDatatoCombobox();
        loadDatatoTable();
        modelImp = new DefaultTableModel();
        String[] cols = {"Mã SP", "Tên SP", "Hãng SX", "Số lượng"};
        modelImp.setColumnIdentifiers(cols);
        //modelPro = new DefaultTableModel();
        modelPro = bllPro.getProducttoImport();
        modelImp.setColumnIdentifiers(cols);
        tbProduct.setModel(modelPro);
        tbImport.setModel(modelImp);
        enableButton();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        ScrollpaneProduct = new javax.swing.JScrollPane();
        tbProduct = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbImport = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        btnAddAll = new javax.swing.JButton();
        btnAddItem = new javax.swing.JButton();
        btnRemoveItem = new javax.swing.JButton();
        btnRemoveAll = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        btnSearch = new javax.swing.JButton();
        txtSearchPro = new javax.swing.JTextField();
        cbSearchManu = new javax.swing.JComboBox();
        txtQuanFrom = new javax.swing.JTextField();
        txtQuanTo = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        lbImportID = new javax.swing.JLabel();
        lbImportName = new javax.swing.JLabel();
        lbImportManu = new javax.swing.JLabel();
        txtImportQuantity = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        lbCurrentQuantity = new javax.swing.JLabel();
        btnAdd = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        txtImportPrice = new javax.swing.JTextField();

        setPreferredSize(new java.awt.Dimension(913, 500));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Danh sách SP", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 0, 102)));
        jPanel1.setPreferredSize(new java.awt.Dimension(390, 23));

        ScrollpaneProduct.setAutoscrolls(true);
        ScrollpaneProduct.setPreferredSize(new java.awt.Dimension(358, 245));

        tbProduct.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tbProduct.setPreferredSize(new java.awt.Dimension(400, 245));
        tbProduct.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbProductMousePressed(evt);
            }
        });
        tbProduct.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tbProductFocusLost(evt);
            }
        });
        ScrollpaneProduct.setViewportView(tbProduct);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ScrollpaneProduct, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ScrollpaneProduct, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Danh sách nhập hàng", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 102, 0)));

        tbImport.setAutoCreateRowSorter(true);
        tbImport.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tbImport.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbImportMousePressed(evt);
            }
        });
        jScrollPane2.setViewportView(tbImport);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 367, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
        );

        btnAddAll.setText(">>");
        btnAddAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddAllActionPerformed(evt);
            }
        });

        btnAddItem.setText(">");
        btnAddItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddItemActionPerformed(evt);
            }
        });

        btnRemoveItem.setText("<");
        btnRemoveItem.setEnabled(false);
        btnRemoveItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveItemActionPerformed(evt);
            }
        });

        btnRemoveAll.setText("<<");
        btnRemoveAll.setEnabled(false);
        btnRemoveAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveAllActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(btnAddItem, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnAddAll, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
                    .addComponent(btnRemoveItem, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnRemoveAll, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent(btnAddItem)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnAddAll)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnRemoveItem)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnRemoveAll)
                .addContainerGap(31, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tìm kiếm", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 0, 102)));

        jLabel2.setText("Tên SP");

        jLabel3.setText("Hãng SX");

        jLabel4.setText("Số lượng từ");

        jLabel5.setText("đến");

        btnSearch.setBackground(new java.awt.Color(153, 153, 153));
        btnSearch.setForeground(new java.awt.Color(51, 51, 51));
        btnSearch.setText("Tìm");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        cbSearchManu.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        txtQuanFrom.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtQuanFromKeyTyped(evt);
            }
        });

        txtQuanTo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtQuanToKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txtSearchPro, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(cbSearchManu, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel4Layout.createSequentialGroup()
                            .addComponent(txtQuanFrom, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jLabel5)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtQuanTo)))
                    .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtSearchPro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cbSearchManu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(txtQuanFrom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtQuanTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSearch)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Nhập hàng", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 102, 0)));

        jLabel6.setText("Mã SP");

        jLabel7.setText("Tên SP");

        jLabel8.setText("Số lượng nhập");

        jLabel9.setText("Hãng SX");

        lbImportID.setText("ID");

        lbImportName.setText("name");

        lbImportManu.setText("manu");

        txtImportQuantity.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtImportQuantityKeyTyped(evt);
            }
        });

        jLabel13.setText("Số lượng trong kho");

        lbCurrentQuantity.setText("quantity");

        btnAdd.setBackground(new java.awt.Color(204, 255, 204));
        btnAdd.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        btnAdd.setForeground(new java.awt.Color(51, 51, 51));
        btnAdd.setText("Thêm");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        jLabel10.setText("Giá nhập 1 sản phẩm");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addComponent(jLabel8)
                    .addComponent(jLabel13)
                    .addComponent(jLabel7)
                    .addComponent(jLabel6)
                    .addComponent(jLabel10))
                .addGap(22, 22, 22)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbImportID, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbImportName, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbImportManu, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbCurrentQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 45, Short.MAX_VALUE))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtImportQuantity)
                            .addComponent(txtImportPrice))
                        .addGap(4, 4, 4)
                        .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(lbImportID)
                        .addGap(0, 0, 0)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbImportName)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbImportManu)
                            .addComponent(jLabel9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbCurrentQuantity)
                            .addComponent(jLabel13))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtImportQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtImportPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnAdd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel10))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(8, 8, 8))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE))
                .addContainerGap(90, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    //method action only allow number input to quantity
    private void txtQuanFromKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtQuanFromKeyTyped
        char c = evt.getKeyChar();
        if(!Character.isDigit(c) || c == KeyEvent.VK_BACK_SPACE || c == KeyEvent.VK_DELETE)
            evt.consume();
    }//GEN-LAST:event_txtQuanFromKeyTyped

    //method action only allow number input to quantity
    private void txtQuanToKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtQuanToKeyTyped
        char c = evt.getKeyChar();
        if(!Character.isDigit(c) || c == KeyEvent.VK_BACK_SPACE || c == KeyEvent.VK_DELETE)
            evt.consume();
    }//GEN-LAST:event_txtQuanToKeyTyped

    //method search product to import
    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        int quanFrom = -1;
        int quanTo = -1;
        if(!"".equals(txtQuanFrom.getText().trim()))
            quanFrom = Integer.valueOf(txtQuanFrom.getText());
        if(!"".equals(txtQuanTo.getText().trim()))
            quanTo = Integer.valueOf(txtQuanTo.getText());
        String manu = cbSearchManu.getSelectedItem().toString();
        modelPro = bllPro.searchProducttoImport(txtSearchPro.getText().trim(), manu, quanFrom, quanTo);
        tbProduct.setModel(modelPro);
        enableButton();
    }//GEN-LAST:event_btnSearchActionPerformed

    private void tbProductMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbProductMousePressed
        //btnAddItem.setEnabled(true);
    }//GEN-LAST:event_tbProductMousePressed

    private void tbProductFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tbProductFocusLost
       //btnAddItem.setEnabled(false);
    }//GEN-LAST:event_tbProductFocusLost

    //method add all item to import
    private void btnAddAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddAllActionPerformed
        try{
            DefaultTableModel model = (DefaultTableModel)tbProduct.getModel();
            DefaultTableModel modelIm = (DefaultTableModel)tbImport.getModel();
            if(model != null)
            {
                int rows = tbProduct.getRowCount();
                if(rows > 0){
                    //modelImp = new DefaultTableModel();
                    for(int i = rows - 1; i >= 0; i--){
                        int id = (int)tbProduct.getValueAt(i, 0);
                        if(!checkExist(id, tbImport)){
                            DTOProduct dtoProduct = bllPro.getProductbyID(id);
                            modelIm.addRow(new Object[]{dtoProduct.getProductID(), dtoProduct.getProductName(),dtoProduct.getManu().getManuName(), dtoProduct.getProductQuantity()});
                            
                        }
                        model.removeRow(i);
                    }
//                    tbImport.setModel(modelImp);
                    
//                    for(int i = rows - 1; i >= 0; i--){
//                        model.removeRow(i);
//                    }
                }
                
                enableButton();
            }
            
        }
        catch(Exception ex)
        {
            JOptionPane.showConfirmDialog(null, "Error");
        }
    }//GEN-LAST:event_btnAddAllActionPerformed

    //method action add item to import
    private void btnAddItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddItemActionPerformed
        try{
            DefaultTableModel model = (DefaultTableModel)tbProduct.getModel();
            DefaultTableModel modelIm = (DefaultTableModel)tbImport.getModel();
            int[] rows = tbProduct.getSelectedRows();
            for(int i = rows.length - 1; i >= 0; i--)
            {
                int id = Integer.valueOf(tbProduct.getValueAt(rows[i], 0).toString());
                if(!checkExist(id, tbImport)){
                    DTOProduct Pro = bllPro.getProductbyID(id);
                    modelIm.addRow(new Object[]{Pro.getProductID(), Pro.getProductName(), Pro.getManu().getManuName(), Pro.getProductQuantity()});
                    
                }
                
            }
            for(int i = rows.length - 1; i >= 0; i--)
            {
                model.removeRow(rows[i]);
            }
            //tbImport.setModel(modelImp);
            enableButton();
        }
        catch(Exception ex)
        {
            System.err.printf(ex.getMessage());
        }
    }//GEN-LAST:event_btnAddItemActionPerformed

    //method action remove all item in import
    private void btnRemoveAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveAllActionPerformed
        DefaultTableModel modelIm = (DefaultTableModel)tbImport.getModel();
        DefaultTableModel modelPr = (DefaultTableModel)tbProduct.getModel();
        if(modelIm != null)
        {
            int rows = tbImport.getRowCount();
            
            for(int i = rows - 1; i >= 0; i--){
                int id = (int)tbImport.getValueAt(i, 0);
                if(!checkExist(id, tbProduct)){
                    DTOProduct dtoPro = bllPro.getProductbyID(id);
                    modelPr.addRow(new Object[]{dtoPro.getProductID(), dtoPro.getProductName(), dtoPro.getManu().getManuName(), dtoPro.getProductQuantity()});
                }
                modelIm.removeRow(i);
            }
//            tbProduct.setModel(modelPro);
            //loadDatatoTable();
            enableButton();
        }
    }//GEN-LAST:event_btnRemoveAllActionPerformed

    //method action remove one item in import
    private void btnRemoveItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveItemActionPerformed
        try
        {
            DefaultTableModel model = (DefaultTableModel)tbImport.getModel();
            DefaultTableModel modelPr = (DefaultTableModel)tbProduct.getModel();
            int[] rows = tbImport.getSelectedRows();
            for(int i = rows.length - 1; i >= 0; i--)
            {
                int id = Integer.valueOf(tbImport.getValueAt(rows[i], 0).toString());
                if(!checkExist(id, tbProduct)){
                    DTOProduct Pro = bllPro.getProductbyID(id);
                    modelPr.addRow(new Object[]{Pro.getProductID(), Pro.getProductName(), Pro.getManu().getManuName(), Pro.getProductQuantity()});
                    
                }
            }
            for(int i = rows.length - 1; i >= 0; i--)
            {
                model.removeRow(rows[i]);
            }
            //tbProduct.setModel(modelPro);
            enableButton();
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }//GEN-LAST:event_btnRemoveItemActionPerformed

    //method action save new import
    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
       try
       {
           int countRow = tbImport.getRowCount();
           if(countRow > 0){
               int rowSeclect = tbImport.getSelectedRow();
               if(rowSeclect > -1){
                   int id = (int)tbImport.getValueAt(tbImport.getSelectedRow(), 0);
                   if("".equals(checkValid())){
                       DTOImport dtoImp = new DTOImport();//contructor object
                       DefaultTableModel modelIm = (DefaultTableModel)tbImport.getModel();
                       int quantity = Integer.valueOf(txtImportQuantity.getText().trim());
                       long price = Long.valueOf(txtImportPrice.getText().trim());
                       Date dateImp = new Date();
                       
                       dtoImp.setImportQuantity(quantity);
                       dtoImp.setImportPrice(price);
                       dtoImp.setDtoPro(bllPro.getProductbyID(id));
                       dtoImp.setImportDate(dateImp);
                       
                       if(bllImp.insertRowImport(dtoImp)){
                           modelIm.removeRow(tbImport.getSelectedRow());
                           lbCurrentQuantity.setText("");
                           lbImportID.setText("");
                           lbImportManu.setText("");
                           lbImportName.setText("");
                           txtImportPrice.setText("");
                           txtImportQuantity.setText("");
                           JOptionPane.showMessageDialog(null, "Đã nhập hàng");
                       }
                           
                   }
                   else
                       JOptionPane.showMessageDialog(null, checkValid());
               }
               else
                   JOptionPane.showMessageDialog(null, "Chọn sản phẩm cần nhập");
           }
       }
       catch(Exception ex){
           JOptionPane.showMessageDialog(null, ex.getMessage());
       }
    }//GEN-LAST:event_btnAddActionPerformed

    private void tbImportMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbImportMousePressed
        int countRow = tbImport.getRowCount();
           if(countRow > 0){
               int rowSeclect = tbImport.getSelectedRow();
               if(rowSeclect > -1){
                   int id = (int)tbImport.getValueAt(tbImport.getSelectedRow(), 0);
                   DTOProduct pro = bllPro.getProductbyID(id);
                   lbImportID.setText(String.valueOf(pro.getProductID()));
                   lbImportName.setText(pro.getProductName());
                   lbImportManu.setText(pro.getManu().getManuName());
                   lbCurrentQuantity.setText(String.valueOf(pro.getProductQuantity()));
               }
               
           }
    }//GEN-LAST:event_tbImportMousePressed

    private void txtImportQuantityKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtImportQuantityKeyTyped
        char c = evt.getKeyChar();
        if(!Character.isDigit(c) || c == KeyEvent.VK_BACK_SPACE || c == KeyEvent.VK_DELETE)
            evt.consume();
    }//GEN-LAST:event_txtImportQuantityKeyTyped

    //method load data to combobox
    private void loadDatatoCombobox()
    {
//        DefaultComboBoxModel model1 = bllManu.getDcbModelManu();
//        cbSearchManu.setModel(model1);
//        cbManu.addItem(new ComboItem("1", "banana"));
//        cbManu.addItem(new ComboItem("2", "apple"));
//        cbManu.addItem(new ComboItem("3", "fruilt"));
        DefaultComboBoxModel model2 = bllManu.getDcbModelManu();
        model2.addElement("Tất cả các hãng");
        cbSearchManu.setModel(model2);
        cbSearchManu.setSelectedItem("Tất cả các hãng");
    }
    
    //method load data to table
    private void loadDatatoTable()
    {
        modelPro = bllPro.getProducttoImport();
        tbProduct.setModel(modelPro);
    }
    
    //method enable button
    private void enableButton()
    {
        
        int countPro = tbProduct.getRowCount();
        int countIm = tbImport.getRowCount();
        if(countPro <= 0)
        {
            btnAddAll.setEnabled(false);
            btnAddItem.setEnabled(false);
        }
        else
        {
            btnAddAll.setEnabled(true);
            btnAddItem.setEnabled(true);
        }
        if(countIm <= 0)
        {
            btnRemoveAll.setEnabled(false);
            btnRemoveItem.setEnabled(false);
        }
        else
        {
            btnRemoveAll.setEnabled(true);
            btnRemoveItem.setEnabled(true);
        }
        
    }
    
    //method check valid input
    private String checkValid()
    {
        String result = "";
        if("".equals(txtImportQuantity.getText()))
            result += "Nhập số lượng\n";
        if("".equals(txtImportPrice.getText().trim()))
            result += "Nhập giá 1 sản phẩm";
        return result;
    }
    //method check exist of item in table
    private boolean checkExist(int id, JTable table)
    {
        int countRow = table.getRowCount();
        if(countRow > 0)
        {
            for(int i = countRow - 1; i >= 0; i--){
                if((int)table.getValueAt(i, 0) == id)
                    return true;
            }
        }
        return false;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane ScrollpaneProduct;
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnAddAll;
    private javax.swing.JButton btnAddItem;
    private javax.swing.JButton btnRemoveAll;
    private javax.swing.JButton btnRemoveItem;
    private javax.swing.JButton btnSearch;
    private javax.swing.JComboBox cbSearchManu;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lbCurrentQuantity;
    private javax.swing.JLabel lbImportID;
    private javax.swing.JLabel lbImportManu;
    private javax.swing.JLabel lbImportName;
    private javax.swing.JTable tbImport;
    private javax.swing.JTable tbProduct;
    private javax.swing.JTextField txtImportPrice;
    private javax.swing.JTextField txtImportQuantity;
    private javax.swing.JTextField txtQuanFrom;
    private javax.swing.JTextField txtQuanTo;
    private javax.swing.JTextField txtSearchPro;
    // End of variables declaration//GEN-END:variables
}
